package com.yufubao.api.controller.applet.authorization;


import com.yufubao.framework.annotation.AutAccess;
import com.yufubao.framework.annotation.SystemLog;
import com.yufubao.framework.constant.NotesConstant;
import com.yufubao.framework.interceptor.local.LoginUserLoc;
import com.yufubao.framework.result.ResponseResult;
import com.yufubao.model.dto.applet.authorization.AppWxLoginDto;
import com.yufubao.model.dto.applet.authorization.SupplierApplyDto;
import com.yufubao.model.dto.common.PhoneLoginDto;
import com.yufubao.service.service.applet.authorization.UserWxPowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/8/26 11:49
 */
@Api("微信小程序权限api")
@RestController
@RequestMapping("/applet/power")
public class UserWxPowerController {

    @Autowired
    private UserWxPowerService userWxPowerService;


    /**
     * 微信小程序登录
     */
    @AutAccess
    @ApiOperation(value = "微信小程序用户登录", notes = NotesConstant.TEST)
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @SystemLog(info = "微信小程序用户登录")
    public ResponseResult login(@RequestBody @Valid AppWxLoginDto dto) {
        /*
          方案一、
          1.小程序前端调用 wx.pluginLogin() 方法获取 code(loginCode)
          2.前端传入code字段给后端login接口
          3.后端调用 wxMaService.getUserService().getSessionInfo(code) 方法获取 session_key 、 openid 、 unionid
          4.后端根据 openid、unionid 查询用户信息，如果用户不存在则创建新用户
          5.后端返回 token和用户信息 给小程序前端，前端保存token，后续每次调用接口都要带上
          	此方案不能获取到用户手机号，需要用户自己绑定
         */
        LoginUserLoc result = userWxPowerService.login(dto);
        return new ResponseResult().ok(result);
    }

    /**
     * 手机号登录
     */
    @AutAccess
    @ApiOperation(value = "手机号登录", notes = NotesConstant.TEST)
    @RequestMapping(value = "/phoneLogin", method = RequestMethod.POST)
    @SystemLog(info = "手机号登录")
    public ResponseResult phoneLogin(@RequestBody @Valid PhoneLoginDto dto) {
        return userWxPowerService.phoneLogin(dto);
    }


    /**
     * 用户申请商户
     */
    @ApiOperation(value = "用户申请商户", notes = NotesConstant.DEV)
    @RequestMapping(value = "/merchantApplication", method = RequestMethod.POST)
    @SystemLog(info = "用户申请商户")
    public ResponseResult merchantApplication(@RequestBody @Valid SupplierApplyDto dto) {
        return userWxPowerService.merchantApplication(dto);
    }

}
